package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Region;
import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.security.auth.login.AppConfigurationEntry;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * 地区资源控制器
 */
@Controller
@RequestMapping("/regions")
public class RegionController {
    @Autowired
    private IRegionService regionService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;

    /*查询所有地区*/
    @GetMapping
    @ResponseBody
    public List<Region> getAllArea(Integer state) {
        List<Region> list = regionService.selectAll(state);
        return list;
    }

    @Autowired
    private ServletContext servletContext;
    @GetMapping(value = "/{regionId}/strategies",produces = "text/html;charset=utf-8")
    @ResponseBody
    public void queryStrategiesByRegionId(StrategyCatalogQueryObject qo, HttpServletResponse response) throws Exception {
        // 查询该地区的所有状态为推荐的攻略(不分页)
        qo.setState(Strategy.STATE_HOT);
        qo.setPageSize(0);
        PageInfo<Strategy> hotList = strategyService.query(qo);
        // 查询地区的所有攻略(分页)
        qo.setState(null);
        qo.setPageSize(4);
        PageInfo<Strategy> allList = strategyService.query(qo);
        // 创建freeMarker的配置对象
        Configuration config = new Configuration(Configuration.VERSION_2_3_23);
        //设置模板的所在目录 / 默认的路径是从app这个项目下开始赵,所以要配置一个路径
        config.setDirectoryForTemplateLoading(new File(servletContext.getRealPath("/template")));
        //设置编码
        config.setDefaultEncoding("utf-8");
        //获取模板
        Template template = config.getTemplate("strategyTemplate.ftl");
        HashMap<String, Object> map = new HashMap<>();
        map.put("hot", hotList.getList());
        map.put("all", allList.getList());
        // 模板和数据合成
        response.setContentType("text/html;charset=utf-8");
        template.process(map, response.getWriter());

    }
    @GetMapping(value = "/{regionId}/strategies",produces = "application/json;charset=utf-8")
    @ResponseBody
    public PageInfo queryStrategiesByRegionId(StrategyCatalogQueryObject qo){
        PageInfo<Strategy> pageInfo = strategyService.query(qo);
        return  pageInfo;
    }
}
